package com.east.service.system.impl;


import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authc.UnknownAccountException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.east.bean.Privilege;
import com.east.bean.Role;
import com.east.bean.User;
import com.east.dao.PrivilegeMapper;
import com.east.dao.RoleMapper;
import com.east.dao.UserMapper;
import com.east.service.system.LoginService;
import com.east.utils.tree.TreeUtil;

@Service("loginService")
public class LoginServiceImpl implements LoginService{
	
	private static final Logger logger = LoggerFactory.getLogger(LoginServiceImpl.class);
	@Autowired
	UserMapper userMapper;
	@Autowired
	RoleMapper roleMapper;
	@Autowired
	PrivilegeMapper privilegeMapper;
	@Autowired
    private RedisTemplate redisTemplate;
	@Override
	public User findByLogin(String account, String pwd) {
		User finByLogin = userMapper.finByLogin(account, pwd);
		return finByLogin;
	}

	@SuppressWarnings("unlikely-arg-type")
	@Override
	public User getUser(User user) {
		User finByLogin = userMapper.finByLogin(user.getuAccount(),user.getuPassword());
		if(finByLogin==null) {
				throw new UnknownAccountException();
		}
		return finByLogin;
	}
	@Async("asyncServiceExecutor")
	@Override
	public List<Privilege> findByPrivilege(User user) {
		List<Integer> finByRolePriv = roleMapper.finByRolePrivList(user.getuId());
		List<Privilege> findByPrivIntegerTree = privilegeMapper.findByPrivIntegerTree(finByRolePriv);
		List<Privilege> prilist=new ArrayList<Privilege>();
		for (Privilege pr : findByPrivIntegerTree) {
			if(pr.getpStyle().equals(1)) {
				prilist.add(pr);
			}
			if(!pr.getpPid().equals(null)&&!pr.getpPid().equals("")) {
				Privilege privilegeFarther = getPrivilegeFarther(prilist,pr.getpPid());
				if(privilegeFarther!=null) {
					prilist.add(privilegeFarther);
				}
			}
				
		}
		List<Privilege> removeDuplicate = removeDuplicate(prilist);
		List<Privilege> childTree = new TreeUtil<Privilege>().getChildTree(removeDuplicate);
		return childTree;
	}
	/**
	 * 去重
	 * @param list
	 * @return
	 */
	public   static   List<Privilege>  removeDuplicate(List<Privilege> list)  {       
		  for  ( int  i  =   0 ; i  <  list.size()  -   1 ; i ++ )  {       
		      for  ( int  j  =  list.size()  -   1 ; j  >  i; j -- )  {       
		           if  (list.get(j).getpId().equals(list.get(i).getpId()))  {       
		              list.remove(j);       
		            }        
		        }        
		      }        
		    return list;       
		}  
	@Async("asyncServiceExecutor")
	public  Privilege getPrivilegeFarther(List<Privilege> prilist,Integer id) {
		
		Privilege selectByPrimaryKey = privilegeMapper.selectByPrimaryKey(id);
		if(selectByPrimaryKey!=null) {
				prilist.add(selectByPrimaryKey);	
				Privilege privilegeFarther = getPrivilegeFarther(prilist,selectByPrimaryKey.getpPid());
				
				if(privilegeFarther!=null) {
					prilist.add(privilegeFarther );
				}
				return selectByPrimaryKey;
			
		}
		return null;
	}
	@Override
	public User findPermissionsByUserId(Integer id) {
		return userMapper.selectByPrimaryKey(id);
	}
	@Async("asyncServiceExecutor")
	@Override
	public List<Role> findByRolePrivile(Integer id) {
		return roleMapper.finByRolePriv(id);
	}

}
